package com.sfl.api_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfl.api_backend.constant.HttpCode;
import com.sfl.api_backend.entity.Labelsets;
import com.sfl.api_backend.mapper.LabelsetsMapper;
import com.sfl.api_backend.service.ILabelsetsService;
import com.sfl.api_backend.utils.Api.RestResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 标签集表 服务实现类
 * </p>
 *
 * @author szc
 * @since 2023-01-27
 */
@Service
public class LabelsetsServiceImpl extends ServiceImpl<LabelsetsMapper, Labelsets> implements ILabelsetsService {

    @Resource
    private LabelsetsMapper labelsetsMapper;

    @Override
    public RestResult analysis(Labelsets vo) {
        try(Reader reader = new InputStreamReader(new FileInputStream(new File(labelsetsMapper.selectById(vo.getLabelsetId()).getLabelsetStatistics())), "utf-8")) {
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while((ch = reader.read()) != -1){
                sb.append((char) ch);
            }
            Map<String, Object> map = new HashMap<>();

            map.put("data", sb);

            return RestResult.success(map);
        }catch (IOException e){
            e.printStackTrace();
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, "系统异常");
        }
    }
}
